//
//  EWebViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/22.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "EWebViewController.h"

@interface EWebViewController ()<UIWebViewDelegate>

@property (nonatomic, assign) BOOL isCollect;

@end

@implementation EWebViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_DetailID release];
  
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.navigationItem.title = @"礼遇详情";
    
    
    //收藏
   self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouWhite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shou:)]autorelease];
    
    
    [self createSubView];
}


#pragma mark - ------------ 创建web子视图 ---------------
- (void)createSubView
{
    //UIWebView 加载网页数据 直接适配屏幕

    
    UIWebView *wv = [[UIWebView alloc]initWithFrame:CGRectMake(0, -50,self.view.frame.size.width, self.view.frame.size.height + 50)];
    [self.view addSubview:wv];
    
    //加载网址
    NSString *url = [NSString stringWithFormat:@"https://enjoy.ricebook.com/product/%@", _DetailID];
    wv.delegate = self;
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    
    wv.scalesPageToFit = YES;

    [wv release];

}

#pragma mark - ------------ 收藏 ---------------
- (void)shou:(UIBarButtonItem *)item
{
    //判断是否选中
    self.isCollect = ! self.isCollect;
    
    //如果选中 心变红色 判断 这个值是否点击过 若收藏过直接返回 未收藏 在去存数据库
    //反选 则 取消收藏 从数据库 移除
    if (self.isCollect) {
       
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shou:)];
   
        NSArray *arr = [[XrDataBaseManager shareInstace]select];
        NSMutableArray *arrTemp = [NSMutableArray array];
        for (EnjoyModel *m in arr) {
         
            [arrTemp addObject:m.short_name];
         }
            //判断是否收藏过
            if ([arrTemp containsObject:_model.short_name]) {

                //代表已经存到数据库 无需再存
                return;
            }
            else{
            
                
                UIAlertController *alertshou = [UIAlertController alertControllerWithTitle:@"已收藏" message:@"请到我的里查看" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:alertshou animated:YES completion:^{
                
                    //添加数据 收藏
                    [[XrDataBaseManager shareInstace]insertEnjoy:_model];
                }];
                
                //自动收回
                
              
                [self performSelector:@selector(autoBackHome:) withObject:alertshou afterDelay:1.5];
           
            }
            
        }

else{
    
    //有值 取消收藏
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouWhite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shou:)];
        
        UIAlertController *alertshou = [UIAlertController alertControllerWithTitle:@"提示" message:@"已取消收藏" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertshou animated:YES completion:^{
           
        }];
        // 取消收藏
        [[XrDataBaseManager shareInstace]deleteTitle:_model.short_name];
        //自动收回
        [self performSelector:@selector(autoBackHome:) withObject:alertshou afterDelay:1.5];
    }
    
}

#pragma mark - ------------ alert自动收回 ---------------
- (void)autoBackHome:(UIAlertController *)alert
{
    
    [alert dismissViewControllerAnimated:YES completion:nil];
    
}


#pragma mark - ------------ tab隐藏 ---------------
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
    self.tabBarController.tabBar.hidden = YES;
    
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
}

#pragma mark - ------------ 视图即将出现 ---------------
- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    NSArray *arr = [[XrDataBaseManager shareInstace]select];
    NSMutableArray *arrTemp = [NSMutableArray array];
    
    for (EnjoyModel *m in arr) {
        [arrTemp addObject:m.short_name];
    
    }
        //如果存过 则图标红色
        if ([arrTemp containsObject: _model.short_name]) {
            
              self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(appear:)];
        }else{
        //未收藏过 则图标为白色
              self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouWhite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shou:)];
       
        
    }
}
/** 如果显示红色 再点击 则提示是否取消收藏 */
- (void)appear:(UIBarButtonItem *)appearItem
{
    //[[XrDataBaseManager shareInstace]deleteTitle:_model.title];
    
    //提示
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消收藏" preferredStyle:UIAlertControllerStyleAlert];
    

    //确定取消收藏
    UIAlertAction *cacel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouWhite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shou:)];
        // 取消收藏
        [[XrDataBaseManager shareInstace]deleteTitle:_model.short_name];

    }];
    
    //取消点击 还显示红色
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shouRed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shou:)];
        
    }];
    
    
    [alert addAction:ok];
    [alert addAction:cacel];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
